package com.ksd.modules.channel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.channel.po.AdminChannelSavePO;
import com.ksd.modules.channel.po.AdminChannelSearchPO;
import com.ksd.modules.channel.service.IAdminChannelService;
import com.ksd.modules.channel.vo.AdminChannelVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminChannelController extends BaseController {
    
    @Autowired
    private IAdminChannelService adminChannelService;

    /**
     * 查询首页有栏目接口
     * @return
     */
    @PostMapping("/channel/list")
    public R<Page<AdminChannelVo>> findAdminChannels(@Validated @RequestBody AdminChannelSearchPO adminChannelSearchPO){
        return R.data(adminChannelService.findAdminChannels(adminChannelSearchPO));
    }

    /**
     * 回收站
     * @return
     */
    @PostMapping("/channel/recyle")
    public R<Page<AdminChannelVo>> findAdminChannelsRecyle(@Validated @RequestBody AdminChannelSearchPO adminChannelSearchPO){
        return R.data(adminChannelService.findAdminChannelsRecyle(adminChannelSearchPO));
    }

    /**
     * 保存和更新
     * @param adminChannelSavePO
     * @return
     */
    @PostMapping("/channel/saveupdate")
    @ApiLog("栏目保存和更新")
    @RepeatSubmit
    public R<AdminChannelVo> saveUpdateAdminChannel(@Validated @RequestBody AdminChannelSavePO adminChannelSavePO){
        return R.data(adminChannelService.saveUpdateAdminChannel(adminChannelSavePO));
    }

    /**
     * 根据id查询栏目
     * @param id
     * @return
     */
    @PostMapping("/channel/get/{id}")
    public R<AdminChannelVo> getAdminChannelById(@PathVariable("id")Long id){
        return R.data(adminChannelService.getAdminChannelById(id));
    }

    /**
     * 根据id删除栏目 --逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/channel/del/{id}")
    @ApiLog("根据id删除栏目")
    public R<Boolean> deleteAdminChannelById(@PathVariable("id")Long id){
        return R.data(adminChannelService.deleteAdminChannelById(id));
    }

    /**
     * 恢复删除
     *
     * @param id
     * @return
     */
    @PostMapping("/channel/recover/{id}")
    @ApiLog("根据id恢复删除")
    @RepeatSubmit
    public R<Boolean> recoverByIdAdminChannel(@PathVariable("id") Long id) {
        return R.data(adminChannelService.recoverByIdAdminChannel(id));
    }

    /**
     * 批量逻辑删除
     *
     * @return
     */
    @PostMapping("/channel/batch/dels")
    @ApiLog("批量逻辑删除")
    @RepeatSubmit
    @ApiOperation("批量逻辑删除")
    public R<Boolean> bacthDeletesAdminChannel(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminChannelService.bacthDeletesAdminChannel(batchIdsPO));
    }


    /**
     * 批量恢复
     *
     * @return
     */
    @PostMapping("/channel/batch/recover")
    @ApiLog("批量恢复")
    @RepeatSubmit
    @ApiOperation("批量恢复")
    public R<Boolean> batchRecoversAdminChannel(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminChannelService.batchRecoversAdminChannel(batchIdsPO));
    }
    
    /**
     * 批量物理删除
     *
     * @return
     */
    @PostMapping("/channel/batch/remove")
    @ApiLog("批量彻底删除")
    @RepeatSubmit
    @ApiOperation("批量彻底删除")
    public R<Boolean> batchRemovesAdminChannel(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminChannelService.batchRemovesAdminChannel(batchIdsPO));
    }

    /**
     * 单个物理删除
     * @param id
     * @return
     */
    @PostMapping("/channel/remove/{id}")
    @ApiLog("根据id删除栏目")
    public R<Boolean> removeAdminChannelById(@PathVariable("id")Long id){
        return R.data(adminChannelService.removeAdminChannelById(id));
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/channel/status/{id}/{field}")
    @ApiLog("栏目修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminChannelService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminChannelService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【栏目】-  复制数据
     * @param opid
     * @return
     */
    @PostMapping("/channel/copy/{opid}")
    @ApiLog("【栏目 复制数据")
    @RepeatSubmit
    public R<Boolean> copyData(@PathVariable("opid")Long opid){
        return R.data(adminChannelService.copyData(opid));
    }
}
